package cn.cmvideo.springboot.algorithm;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 
 * @author Administrator
 *
 */
public class StrLength
{
	/**
	 * 给定一个字符串，找出其中不含有重复字符的 最长子串 的长度。
	 * 
	 * @param str
	 * @return
	 */
	public static int lengthOfLongestSubstring(String str)
	{
		int l = str.length();
		int ans = 0;
		Map<Character, Integer> map = new HashMap<Character, Integer>();

		for (int j = 0, i = 0; j < l; j++)
		{
			if (map.containsKey(str.charAt(j)))
			{
				i = Math.max(map.get(str.charAt(j)), i);
			}
			ans = Math.max(ans, (j + 1) - i);
			map.put(str.charAt(j), j + 1);
		}

		return ans;
	}

	/**
	 * 给定一个字符串，找出其中不含有重复字符的 最长子串。
	 * 
	 * @param str
	 * @return
	 */
	public static String longestSubstring(String str)
	{
		int l = str.length();
		int ans = 0;
		Map<Character, Integer> map = new HashMap<Character, Integer>();
		Map<String, Integer> map2 = new HashMap<String, Integer>();
		StringBuffer sb = new StringBuffer();

		for (int j = 0, i = 0; j < l; j++)
		{
			if (map.containsKey(str.charAt(j)))
			{
				if (ans < (j + 1) - i)
				{
					// 记录字符串从第几个字符累积
					map2.put("index", i);
				}
				i = Math.max(map.get(str.charAt(j)), i);
			}

			ans = Math.max(ans, (j + 1) - i);
			map.put(str.charAt(j), j + 1);
		}

		int index = map2.get("index");
		for (int j = 0; j < ans; j++)
		{
			sb.append(str.charAt(index + j));
		}
		return sb.toString();
	}

	public static void main(String[] args)
	{
		String str = "abbcdedffghijkmmmllpoqwxyzw";
		int lengthOfLongestSubstring = lengthOfLongestSubstring(str);
		String longestSubstring = longestSubstring(str);
		System.out.println(lengthOfLongestSubstring);

		System.out.println(longestSubstring);
		
		System.out.println(1897%10);
		System.out.println(1897/10 + "--" +(1897/10)%10);
		System.out.println((1897/100)%10);
		System.out.println((1897/1000)%10);
		ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
//		ThreadPoolExecutor pool = new Thread
	}

}
